package com.easytouch.core.task;

import android.util.Pair;

import com.easytouch.core.DriverThread;
import com.easytouch.core.model.GoodsModel;
import com.easytouch.core.utils.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by gulin on 2017/8/18.
 */

public class AvmPayTask extends Task {
    private Log mLogger = new Log("AvmPayTask.txt");
    private GoodsModel mGoods = null;
    private String orderID;
    private String mType;
    private int mAmount = 1;

    public AvmPayTask(final String pOrderNo, final GoodsModel pGoods, final String pType) {
        this(pOrderNo, pGoods, pType, 1);
    }

    public AvmPayTask(final String pOrderNo, final GoodsModel pGoods, final String pType, final int pAmount) {
        super(pOrderNo);
        String order = pOrderNo;
        String[] split = pOrderNo.split("-");
        if(split != null)
        {
            order = split[0];
        }
        mGoods = pGoods;
        orderID = order;
        mType = pType;
        mAmount = pAmount;
    }

    @Override
    public void run() {
        for (int i = 0;i < mAmount;i++) {
            SimpleDateFormat sd = new SimpleDateFormat("HHmmss", Locale.US);
            String tradeTrace = sd.format(new Date());
            //出货
            if (!DriverThread.getInstance().outGoods(mGoods.getBox(), mGoods.getColumn(), mGoods.getPrice(), mType, tradeTrace, orderID)) {
                //出货失败，发送失败广播
                sendResult(-1, tradeTrace, null);
                cleanup(0, "finish");
                return;
            }
            int step = 0;
            int result = -1;
            try {
                while (step <= 120) {
                    Thread.sleep(500);
                    step++;
                    Pair<Integer, Integer> pair = DriverThread.getInstance().getResult(tradeTrace);
                    if (pair != null) {
                        result = pair.first;
                        break;
                    }
                }
            } catch (InterruptedException e) {
            }
            boolean isOutGoodsSucc = false;
            if (result == 0) { // 出货成功
                isOutGoodsSucc = true;
            } else { // 出货失败
                isOutGoodsSucc = false;
            }
            mLogger.i("isOutGoodsSucc = " + isOutGoodsSucc);
            //出货结果,发送广播
            sendResult(result, tradeTrace, mType);
            if(!isOutGoodsSucc)
            {
                break;
            }
        }
        cleanup(0, "finish");
    }

    private void sendResult(final int pResult, final String pTrace, final String pType)
    {
//        Intent intent = new Intent(INTENT_OUT_GOODS);
//        intent.putExtra(EXTRA_OUT_GOODS_RESULT, pResult);
//        intent.putExtra(EXTRA_OUT_GOODS_TRACE, pTrace);
//        intent.putExtra(EXTRA_OUT_GOODS_TYPE, pType);
//        AppApplication.context.sendBroadcast(intent);
    }

    private void cleanup(final int pResultCode, final String pResultMessage)
    {
        TaskThreadPool.removeIdentifier(getIdentifier());
//        Intent intent = new Intent(INTENT_PICK_RESULT);
//        intent.putExtra(EXTRA_PICK_RESULT_CODE, pResultCode);
//        intent.putExtra(EXTRA_PICK_RESULT_MESSAGE, pResultMessage);
//        intent.putExtra(EXTRA_PICK_CODE, getIdentifier());
//        AppApplication.context.sendBroadcast(intent);
    }
}
